<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * 门店管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$dish_types=array(
	'1'=>'堂点',
	'2'=>'外卖',
	'3'=>'自取',
	'4'=>'排队',
	'5'=>'预约',
);
$services=array(
	'1'=>'wifi',
	'2'=>'刷卡',
	'3'=>'包厢',
	'4'=>'停车',
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title','logo','dish_type','status','createtime');
	$list=pdo_getslice('jy_dish_store',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
		$item['url']=custom_url('jy_dish/store//id='.$item['id'],true);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['dish_types']=$dish_types;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'门店列表'),
			array('op'=>'edit','title'=>'创建门店'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'id','title'=>'门店ID','type'=>'string'),
			array('name'=>'title','title'=>'门店名称','type'=>'string'),
			array('name'=>'logo','title'=>'门店logo','type'=>'image'),
			array('name'=>'dish_type','title'=>'用餐类型','type'=>'checkbox','options'=>'dish_types','set'=>'set'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'status','title'=>'是否开启','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','action'=>'s_count','title'=>'管理'),
		)),
		'search_one'=>array('name'=>'title'),
	);
	$return['htmls']=$htmls;
	if($_RQ['from'] != 'diy'){
		set_html('framess',$this->menu());
	}
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array('dish_types'=>$dish_types,'services'=>$services);
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_dish_store',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['lat_lng']=$item['lat_lng']?json_decode($item['lat_lng'],true):array('lat'=>0);
		$item['images']=$item['images']?json_decode($item['images'],true):array();
		$item['time']=$item['time']?json_decode($item['time'],true):'';
	}else{
		$item=array();
	}
	//print_r($item);exit;
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回门店列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'门店名称','name'=>'title','help'=>''),
					array('type'=>'image','title'=>'门店logo','name'=>'logo','help'=>''),
					array('type'=>'switch','title'=>'显示在列表页','name'=>'show_list','help'=>'开启后，将会显示在门店列表页；否则不显示在门店列表页'),
					array('type'=>'images','title'=>'展示图片','name'=>'images','help'=>'上传门店展示图片'),
					array('type'=>'text','title'=>'门店介绍','name'=>'desc','help'=>'填写门店介绍'),
					array('type'=>'string','title'=>'人均消费','name'=>'money','help'=>''),
					array('type'=>'time_hour','title'=>'营业时间','name'=>'time','help'=>'设置门店营业时间'),
					array('type'=>'string','title'=>'联系电话','name'=>'mobile','help'=>'设置门店联系电话，显示在门店详情页'),
					array('type'=>'string','title'=>'门店地址','name'=>'address','help'=>'设置门店地址，显示在门店详情页'),
					array('type'=>'lat_lng','title'=>'门店坐标','name'=>'lat_lng','help'=>'设置门店坐标'),
					array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>'开启后门店方可用'),
				)
			),
			array(
				'title'=>'运营参数',
				'list'=>array(
					array('type'=>'checkbox','title'=>'用餐类型','name'=>'dish_type','help'=>'选择用餐类型','options'=>'dish_types'),
					array('type'=>'switch','title'=>'支付订单自动确认','name'=>'confirm','help'=>'开启后，顾客支付的订单会自动确认，无需管理员手动确认' ),
					array('type'=>'switch','title'=>'首次下单短信验证','name'=>'sms_code','help'=>'开启后，首次下单的用户将会要求输入短信验证码'),
					array('type'=>'checkbox','title'=>'提供服务','name'=>'service','help'=>'勾选门店提供的服务，将会在门店详情页显示','options'=>'services'),
					array('type'=>'string','title'=>'外卖起送金额','name'=>'out_start','help'=>''),
					array('type'=>'string','title'=>'外卖配送费','name'=>'out_pay','help'=>''),
					array('type'=>'string','title'=>'免配送费金额','name'=>'out_end','help'=>'设置订单金额达到多少元开始免配送费'),
					array('type'=>'string','title'=>'配送半径','name'=>'out_distance','help'=>'单位：千米。设置配送半径，配送距离太远的系统自动提示用户更换用餐方式'),
					array('type'=>'string','title'=>'店内用餐茶位费','name'=>'in_pay','help'=>''),
					array('type'=>'switch','title'=>'店内用餐是否支持加菜','name'=>'in_add','help'=>''),
				),
			)
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'logo'=>trim($postdata['logo']),
		'show_list'=>intval($postdata['show_list']),
		'images'=>ijson_encode($postdata['images']),
		'desc'=>trim($postdata['desc']),
		'money'=>intval($postdata['money']),
		'time'=>ijson_encode($postdata['time']),
		'mobile'=>trim($postdata['mobile']),
		'address'=>trim($postdata['address']),
		'lat_lng'=>ijson_encode($postdata['lat_lng']),
		'status'=>intval($postdata['status']),
		'dish_type'=>trim($postdata['dish_type']),
		'confirm'=>intval($postdata['confirm']),
		'sms_code'=>intval($postdata['sms_code']),
		'service'=>trim($postdata['service']),
		'out_start'=>intval($postdata['out_start']),
		'out_pay'=>intval($postdata['out_pay']),
		'out_end'=>intval($postdata['out_end']),
		'out_distance'=>intval($postdata['out_distance']),
		'in_pay'=>intval($postdata['in_pay']),
		'in_add'=>intval($postdata['in_add']),
	);
	if(!$postdata['title']){
		exi('请输入门店名称！','error');
	}
	if(!$postdata['logo']){
		exi('请选择门店的logo!','error');
	}
	if(!$postdata['images']){
		exi('请上传至少一张展示图片','error');
	}
	if(!$postdata['desc']){
		exi('请简单介绍下您的门店','error');
	}
	if(!$postdata['money']){
		exi('请输入人均消费','error');
	}
	if(!$postdata['time']){
		exi('请选择营业时间','error');
	}
	if(!$postdata['mobile']){
		exi('请输入联系电话','error');
	}
	if(!$postdata['address']){
		exi('请输入门店地址','error');
	}
	if(!$postdata['status'] && !$postdata['out_start'] && !$postdata['out_pay'] && !$postdata['out_end'] && !$postdata['out_distance'] && !$postdata['in_pay']){
		exi('请设置好门店的运营参数','error');
	}
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_dish_store',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_dish_store',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['status'])){
		$update['status']=intval($post['status']);
	}
	if($post['dish_type']){
		$update['dish_type']=trim($post['dish_type']);
	}
	if($update){
		pdo_update('jy_dish_store',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_dish_store',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
